#include<stdio.h>
#include "func.h"
DataType add(DataType num1,DataType num2){
    printf("%d + %d = %d\n",num1,num2,num1+num2);
    return 1;
}
DataType sub(DataType num1,DataType num2){
    printf("%d - %d = %d\n",num1,num2,num1-num2);
    return 1;
}
DataType mul(DataType num1,DataType num2){
    printf("%d * %d = %d\n",num1,num2,num1*num2);
    return 1;
}
DataType divide(DataType num1,DataType num2){
    if(num2!=0){
    printf("%d / %d = %d\n",num1,num2,num1/num2);
        return 1;
    }
    else {
    printf("Error! The num2 can't be zero!\n");
    return 1;}
}
int mod(DataType num1,DataType num2){
    int a;
    if(num2!=0){a=num1%num2;
    printf("%d %% %d = %d\n",num1,num2,a);
    return 1;
}
    else
    printf("Error! The num2 can't be zero!\n");
    return 1;
}
void cal(void){int i=0,n,j,k;char ch,c[1024]={'\0'};
    int a[1024]={0};
    printf("Please input the expression:");
    while((ch=getchar())!='\n'){
        if(ch!=' ') {c[i]=ch;i++;}
    }
    c[i]='\0';
    pre_process(c,a,i);
    n=0;
    while(c[n]!='\0') {n++;}
    process_0(c,a,n);
    for(j=0;j<n;j++) 
    {
        k=delete_large(c,a,n,0,n);
        if(k!=0) {printf("Error!The num2 can't be zero!");return 0;}
    }
    printf("%d\n",a[0]);
    return 0;

}
void opmenu(void){
    printf("* * * * Simple Arithmetic Operation Selection Menu* * * *\n");
    printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * \n");
    printf("1.Addition Operation (+)\n");
    printf("2.Subtraction Operation (-)\n");
    printf("3.Multiplication Operation (*)\n");
    printf("4.Division Operation (/)\n");
    printf("5.Modulo Operation (%%)\n");
    printf("6.Expression Evaluation (#)\n");
    printf("0.Exit (Press Q/q to exit)\n");
    printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * \n");
}

int isValidInput(char ch){
    if((ch=='1')||(ch=='2')||(ch=='3')||(ch=='4')||(ch=='5')||(ch=='6')||(ch=='Q')||(ch=='q')||(ch=='0'))
    return 1;
    else return -1;
}
void clearCache(void){
        char ch;
        while((ch=getchar())!='\n'){;}
}

void pre_process(char c[],int a[],int i){
    int s,k,m,j;
    for(j=0;j<i;j++){s=0;
        if(c[j]>='0'&&c[j]<='9') {k=j;                          //1 2->10*1+2
        while((c[k]>='0')&&(c[k]<='9')) {
            s=c[k]-'0'+10*s;
            k++;}
        a[j]=s;
        for(m=j+1;m<=i;m++){
        c[m]=c[m+k-j-1]; }}
        }}

void process_0(char c[],int a[],int n){
    int h,j;
    int p;
    for(h=0;h<n;h++)                                            //fu shu
    for(p=0;p<n;p++){
        if ((p==0)&&(c[p+1]>='0')&&(c[p+1]<='9')&&(c[p]=='-'))
        {
            a[p]=-1*a[p+1]; c[p]=c[p+1];
            for(j=p+1;j<n;j++)     {c[j]=c[j+1];a[j]=a[j+1];}
        }
        if ((c[p]=='-')&&(c[p-1]!=')')&&((c[p-1]<'0'||c[p-1]>'9'))&&(c[p+1]>='0'&&c[p+1]<='9')&&(p!=1)) {
            a[p]=-1*a[p+1]; c[p]=c[p+1];
            for(j=p+1;j<n;j++)     {c[j]=c[j+1];a[j]=a[j+1];}
        }
    }
}

int calculate(char c[],int a[],int n,int x,int x1){
    int p,j;
    for(p=x;p<x1-2;p++){
        if((c[p+1]>='0')&&(c[p+1]<='9')&&(c[p-1]>='0')&&(c[p-1]<='9')&&(c[p])=='*'){
        a[p-1]=a[p-1]*a[p+1];
        for(j=p;j<n;j++)      {c[j]=c[j+2];a[j]=a[j+2];}
        }
        if((c[p+1]>='0')&&(c[p+1]<='9')&&(c[p-1]>='0')&&(c[p-1]<='9')&&(c[p])=='/'){
        if(a[p+1]!=0){a[p-1]=a[p-1]/a[p+1];
        for(j=p;j<n;j++)      {c[j]=c[j+2];a[j]=a[j+2];}
        }
        else return 1;}
        }    
    for(p=x;p<x1-2;p++){
        if((c[p+1]>='0')&&(c[p+1]<='9')&&(c[p-1]>='0')&&(c[p-1]<='9')&&(c[p])=='+'){
        a[p-1]=a[p-1]+a[p+1];
        for(j=p;j<n;j++)       {c[j]=c[j+2];a[j]=a[j+2];}
        }
        if((c[p+1]>='0')&&(c[p+1]<='9')&&(c[p-1]>='0')&&(c[p-1]<='9')&&(c[p])=='-'){
        a[p-1]=a[p-1]-a[p+1];
        for(j=p;j<n;j++)       {c[j]=c[j+2];a[j]=a[j+2];}
        }}
        return 0;
}

int delete_little(char c[],int a[],int n,int y,int y1){
    int p,j,k=0,k1=0,x,x1,m=0;   
    for(p=y;p<=y1;p++){k1=k;
        if (c[p]=='(') {k++;x=p;}
        if (c[p]==')') {k--;x1=p;}
        if (k1-k==1) {
            for(j=x;j<=x1-2;j++)
            {a[j]=a[j+1];c[j]=c[j+1];}
            for(j=x1-1;j<n;j++)
            {a[j]=a[j+2];c[j]=c[j+2];}
            m+=delete_little(c,a,n,x,x1);
            p=x;

        }
    }
        m+=calculate(c,a,n,y,y1);
        return (m);
}

int delete_mid(char c[],int a[],int n,int y,int y1){
    int p,j,k=0,k1=0,x,x1,m=0;
    for(p=y;p<=y1;p++){k1=k;
        if (c[p]=='[') {k++;x=p;}
        if (c[p]==']') {k--;x1=p;}
        if (k1-k==1) {
            for(j=x;j<=x1-2;j++)
            {a[j]=a[j+1];c[j]=c[j+1];}
            for(j=x1-1;j<n;j++)
            {a[j]=a[j+2];c[j]=c[j+2];}
            m+=delete_mid(c,a,n,x,x1);
            p=x;
        }
}
        m=+delete_little(c,a,n,y,y1);
        return (m);

}
int delete_large(char c[],int a[],int n,int y,int y1){
    int p,j,k=0,k1=0,x,x1,m=0;
    for(p=y;p<=y1;p++){k1=k;
        if (c[p]=='{') {k++;x=p;}
        if (c[p]=='}') {k--;x1=p;}
        if (k1-k==1) {
            for(j=x;j<=x1-2;j++)
            {a[j]=a[j+1];c[j]=c[j+1];}
            for(j=x1-1;j<n;j++)
            {a[j]=a[j+2];c[j]=c[j+2];}
            m+=delete_large(c,a,n,x,x1);
            p=x;
        }
}
        m+=delete_mid(c,a,n,y,y1);
        return (m);

}